package com.youku.plugins.menu.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import flash.ui.ContextMenu;
   import flash.ui.ContextMenuItem;
   import com.youku.events.ConductorEvent;
   import com.youku.conductor.ConductorState;
   import com.youku.VersionInfo;
   import flash.events.ContextMenuEvent;
   import com.youku.events.PluginEvent;
   import com.youku.utils.ReportUtil;
   import com.youku.utils.OpenWindow;
   import com.youku.data.PlayerConstant;
   import flash.events.Event;
   import flash.system.System;
   import com.youku.PlayerConfig;
   
   public class ContextMenuMediator extends Object
   {
      
      public function ContextMenuMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMC = param1;
         this._playerProxy = param2;
         this._inited = false;
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetadataUpdated);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
      }
      
      public static const NAME:String = "ContextMenuMediator";
      
      private var _mainMC:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _inited:Boolean = false;
      
      private var _contextMenu:ContextMenu;
      
      private var _versionItem:ContextMenuItem;
      
      private var _stopItem:ContextMenuItem;
      
      private var _copyItem:ContextMenuItem;
      
      private var _copyTimeItem:ContextMenuItem;
      
      private var _videoinfoItem:ContextMenuItem;
      
      private var _helpItem:ContextMenuItem;
      
      private var _debugItem:ContextMenuItem;
      
      private var _gpuOpenItem:ContextMenuItem;
      
      private var _gpuCloseItem:ContextMenuItem;
      
      private function onStateChanged(param1:ConductorEvent) : void {
         if(!this._inited)
         {
            return;
         }
         this._stopItem.enabled = true;
         this._copyItem.enabled = true;
         this._copyTimeItem.enabled = true;
         this._videoinfoItem.enabled = true;
         this._helpItem.enabled = true;
         if(this._debugItem)
         {
            this._debugItem.enabled = true;
         }
         var _loc2_:String = param1.data as String;
         if(_loc2_ == ConductorState.AD || _loc2_ == ConductorState.VIDEO_END)
         {
            this._videoinfoItem.enabled = false;
         }
      }
      
      private function onMetadataUpdated(param1:ConductorEvent) : void {
         var _loc2_:* = false;
         this._contextMenu = new ContextMenu();
         this._contextMenu.hideBuiltInItems();
         this._versionItem = new ContextMenuItem("version: " + "4.1." + VersionInfo.ver());
         this._contextMenu.customItems.push(this._versionItem);
         this._stopItem = new ContextMenuItem("停止播放",true);
         this._stopItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,this.onStopItemSelect);
         this._contextMenu.customItems.push(this._stopItem);
         this._copyItem = new ContextMenuItem("复制视频地址",true);
         this._copyItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,this.onCopyItemSelect);
         this._contextMenu.customItems.push(this._copyItem);
         this._copyTimeItem = new ContextMenuItem("复制当前时间视频地址",false);
         this._copyTimeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,this.onCopyTimeItemSelect);
         this._contextMenu.customItems.push(this._copyTimeItem);
         this._videoinfoItem = new ContextMenuItem("显示视频信息",true);
         this._videoinfoItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,this.onVideoInfoItemSelect);
         this._contextMenu.customItems.push(this._videoinfoItem);
         this._helpItem = new ContextMenuItem("在线帮助和反馈",false);
         this._helpItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,this.onHelpItemSelect);
         this._contextMenu.customItems.push(this._helpItem);
         if(this._playerProxy.rootGetData.rootWinType == "interior" && !this.has1080P())
         {
            _loc2_ = false;
            if(_loc2_)
            {
               this._gpuCloseItem = new ContextMenuItem("关闭GPU硬件加速",true);
               this._gpuCloseItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,this.onGpuCloseItemSelect);
               this._contextMenu.customItems.push(this._gpuCloseItem);
            }
            else
            {
               this._gpuOpenItem = new ContextMenuItem("开启GPU硬件加速",true);
               this._gpuOpenItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,this.onGpuOpenItemSelect);
               this._contextMenu.customItems.push(this._gpuOpenItem);
            }
         }
         this._inited = true;
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.CM_MENU_UPDATED,{"menu":this._contextMenu}));
      }
      
      private function has1080P() : Boolean {
         var _loc2_:String = null;
         var _loc1_:* = false;
         for each(_loc2_ in this._playerProxy.playerData.playlistTypeArr)
         {
            if(_loc2_ == "hd3")
            {
               _loc1_ = true;
            }
         }
         return _loc1_;
      }
      
      private function onGpuCloseItemSelect(param1:ContextMenuEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.CM_GPU_CLOSE));
         ReportUtil.sendUserActionLog(ReportUtil.MENU_GUP_CLOSE);
      }
      
      private function onGpuOpenItemSelect(param1:ContextMenuEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.CM_GPU_OPEN));
         ReportUtil.sendUserActionLog(ReportUtil.MENU_GUP_OPEN);
      }
      
      private function onVideoInfoItemSelect(param1:ContextMenuEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.CM_VIDEO_INFO));
         ReportUtil.sendUserActionLog(ReportUtil.MENU_VIDEO_INFO);
      }
      
      private function onStopItemSelect(param1:ContextMenuEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.CM_STOP_PLAY));
         ReportUtil.sendUserActionLog(ReportUtil.MENU_STOP);
      }
      
      private function onHelpItemSelect(param1:ContextMenuEvent) : void {
         OpenWindow.openURL(PlayerConstant.YOUKU_HELP_LINK);
         ReportUtil.sendUserActionLog(ReportUtil.MENU_HELP);
      }
      
      private function onCopyItemSelect(param1:Event) : void {
         var _loc2_:String = this._playerProxy.playerData.getVideoURL(false);
         try
         {
            System.setClipboard(_loc2_);
         }
         catch(e:Error)
         {
         }
         ReportUtil.sendUserActionLog(ReportUtil.MENU_COPY_URL);
      }
      
      private function onCopyTimeItemSelect(param1:ContextMenuEvent) : void {
         var _loc2_:String = this._playerProxy.playerData.getVideoURL(false);
         var _loc3_:int = int(this._playerProxy.playerData.videoNsTime);
         _loc2_ = _loc2_ + ("?firsttime=" + _loc3_);
         try
         {
            System.setClipboard(_loc2_);
         }
         catch(e:Error)
         {
         }
         ReportUtil.sendUserActionLog(ReportUtil.MENU_COPY_URL_TIME);
      }
      
      private function onDebugItemSelect(param1:ContextMenuEvent) : void {
         PlayerConfig.printdebug = !PlayerConfig.printdebug;
      }
   }
}
